//
// Treasure Chest Window Dialog
//
// L. Dean Gibson II  2/20/01
//
//

#include "precompiled.h"
#include "treasure_chest_window.h"

#include "adv_treasure_chest.h"
#include "bitmap_group.h"
#include "bitmap_group_cache.h"
#include "bitmap_layer.h"
#include "bitmap_layer_window.h"
#include "bound_handler.h"
#include "button_cache.h"
#include "external_string.h"
#include "format_string.h"
#include "interface_help.h"
#include "materials.h"
#include "platform.h"
#include "replace_keywords.h"
#include "screen_point.h"
#include "skill.h"
#include "skill_button.h"
#include "standard_fonts.h"
#include "text_window.h"
#include "window_rect.h"

extern t_button_cache			    g_ok_button;	   // reference to the global ok button
extern t_bitmap_group_cache const   k_skill_frames;
extern t_external_string    k_text_experience;
extern t_external_string    k_text_gold( "gold" );

//
// constructor
//

t_bitmap_group_cache const			k_treasure_chest_bitmaps( "dialog.treasure_chest" );

t_treasure_chest_window::t_treasure_chest_window( t_window* parent, t_adv_treasure_chest* chest )
					    : t_window( k_completely_transparent, parent )
{
	m_treasure_chest = chest;

	// load images and find dimensions of window.
	t_screen_rect rect;
	t_screen_rect parent_rect = get_parent()->get_client_rect();
	t_button*             button;
	t_help_block const&	    help = get_help_block( "shared" );

	// set the adventure object pointer

	m_chest_bitmaps = k_treasure_chest_bitmaps.get();

	rect = m_chest_bitmaps->get_rect();
	rect += (parent_rect.size() - rect.size()) / 2;
	init( rect );

	// create background
	t_bitmap_layer const* layer;
	t_screen_point        background_origin;
	t_window*             window;
	t_screen_point        zero(0,0);

	layer = m_chest_bitmaps->find( "background" );
	background_origin = layer->get_rect().top_left();
	window = new t_bitmap_layer_window( layer, zero, this );
	m_index_window = new t_window( layer->get_rect() - background_origin, 
		                           k_completely_transparent, window );

	// add the ok button
	layer = m_chest_bitmaps->find( "ok_button" );

	button = new t_button( g_ok_button.get(), layer->get_rect().top_left(), this );
	button->set_click_handler( bound_handler( *this, &t_treasure_chest_window::close_click ) );
  set_help( button, help, "ok" );	

	// create edges
	layer = m_chest_bitmaps->find( "top" );
	window = new t_bitmap_layer_window( layer, zero, this );
	layer = m_chest_bitmaps->find( "left" );
	window = new t_bitmap_layer_window( layer, zero, this );
	layer = m_chest_bitmaps->find( "bottom" );
	window = new t_bitmap_layer_window( layer, zero, this );
	layer = m_chest_bitmaps->find( "right" );
	window = new t_bitmap_layer_window( layer, zero, this );

	// create the labels and buttons for the dialog
	create_buttons( background_origin );
	create_labels( background_origin );
}

//
// create the toggle buttons for the dialog
//
void t_treasure_chest_window::create_buttons( t_screen_point origin )
{
	t_bitmap_group_ptr      skill_frames = k_skill_frames.get();
    t_screen_point          offset;
    t_screen_rect           rect;
	t_help_block const&     material_help = get_help_block( "material_display" );
	t_help_block const&	    shared_help = get_help_block( "shared" );
    
    rect = m_chest_bitmaps->find( "gold_icon" )->get_rect();
	m_gold_button = new t_toggle_button( rect.top_left(), this, true );
    create_frame_button( m_gold_button, skill_frames, m_chest_bitmaps, "gold" );
    offset = skill_frames->find( "icon" )->get_rect().top_left();
	m_gold_button->set_click_handler( bound_handler( *this, &t_treasure_chest_window::gold_change ));
    set_help( m_gold_button, material_help, "gold" );
	m_toggle_group.add( m_gold_button );

    rect = m_chest_bitmaps->find( "experience_icon" )->get_rect();
	m_experience_button = new t_toggle_button( rect.top_left(), this, true );
    create_frame_button( m_experience_button, skill_frames, m_chest_bitmaps, "experience" );
    offset = skill_frames->find( "icon" )->get_rect().top_left();
	m_experience_button->set_click_handler( bound_handler( *this, &t_treasure_chest_window::exp_change ));
    set_help( m_experience_button, shared_help, "experience" );
	m_toggle_group.add( m_experience_button );

	m_toggle_group.select( 0 );
	m_return_value = true; // Select gold first.
}

// create the label borders and the associated text for them
void t_treasure_chest_window::create_labels( t_screen_point book_origin )
{
	t_screen_rect			rect;
//	t_window*				window;
	t_text_window*          text_window;
	t_bitmap_layer const*	layer;
	std::string				text;
	std::string				exp_text;

    // Add title.
	rect = m_chest_bitmaps->find( "title" )->get_rect();
	text_window = new t_text_window( get_font( 20 ), rect, this, 
									 m_treasure_chest->get_text( "name" ),
									 t_pixel_24(0,0,0) );
	text_window->set_drop_shadow( true, t_pixel_24(200,200,200));
	text_window->set_center_horizontal();

	layer = m_chest_bitmaps->find( "dialog_text" );
//	window = new t_bitmap_layer_window( layer, t_screen_point(0,0), this );

	rect = layer->get_rect();
	text_window = new t_text_window( get_font( 20 ), rect, this, 
									 m_treasure_chest->get_text( "Initial" ),
									 t_pixel_24(0,0,0) );

	text_window->set_drop_shadow( true, t_pixel_24(200,200,200));
	text_window->set_center_horizontal();

	layer = m_chest_bitmaps->find( "ok_button" );
	//window = new t_bitmap_layer_window( layer, t_screen_point(0,0), this );

	layer = m_chest_bitmaps->find( "gold_label" );
	//window = new t_bitmap_layer_window( layer, t_screen_point(0,0), this );
	rect = layer->get_rect();


	text = m_treasure_chest->get_text( "gold" );
	text = replace_keywords(text, m_treasure_chest->get_gold(), k_gold);

	text_window = new t_text_window( get_font( 14 ), rect, this, 
		                             text,
									 t_pixel_24(0,0,0) );
	text_window->set_drop_shadow( true, t_pixel_24(200,200,200));
	text_window->set_center_horizontal();


	layer = m_chest_bitmaps->find( "experience_label" );
	//window = new t_bitmap_layer_window( layer, t_screen_point(0,0), this );
	rect = layer->get_rect();

	text = m_treasure_chest->get_text( "experience" );
	exp_text = format_string( "%i", m_treasure_chest->get_gold() - 500 );

	text = replace_keywords(text, "%experience", exp_text);
	text_window = new t_text_window( get_font( 14 ), rect, this, 
		                             text,
									 t_pixel_24(0,0,0) );
	text_window->set_drop_shadow( true, t_pixel_24(200,200,200));
	text_window->set_center_horizontal();

}

//
// exp button status has changed
//
void t_treasure_chest_window::exp_change( t_button* button)
{
	if (button->is_pressed())
		m_return_value = true;
	else
		m_return_value = false;

}

//
// gold button status has changed
//
void t_treasure_chest_window::gold_change( t_button* button )
{
	if (button->is_pressed())
		m_return_value = false;
	else
		m_return_value = true;

}

// -------------------------------------------------------------------
// the close button has been clicked
// -------------------------------------------------------------------
void t_treasure_chest_window::close_click( t_button* button )
{
	close(); 
}


